package com.zyx.mbg;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

/**
 * <p>解决mapper无法生成注释</p>
 * <p>&emsp;&emsp;https://www.code456.com/article/1603511.html</p>
 *
 * @author Yaxi.Zhang
 * @since 2022/8/28 07:20
 */
public class MapperCommentGenerator extends PluginAdapter {
    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String author = properties.getProperty("author", "zyx");
        // 继承类
        // FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("Mapper<"
        //         + introspectedTable.getBaseRecordType() + ">");
        // 导包类
        // FullyQualifiedJavaType mapper = new FullyQualifiedJavaType("com.github.abel533.mapper.Mapper");
        FullyQualifiedJavaType component = new FullyQualifiedJavaType("org.springframework.stereotype.Component");
        // extends MybatisBaseMapper
        // interfaze.addSuperInterface(fqjt);
        // 开始导包
        // interfaze.addImportedType(mapper);
        interfaze.addImportedType(component);
        // 清除原有方法
        // interfaze.getMethods().clear();
        // 清除原有注释
        // interfaze.getAnnotations().clear();
        // 根据业务自己灵活发挥，本次需要spring注入
        interfaze.addAnnotation("@Component(\"" + introspectedTable.getFullyQualifiedTable() + "\")");
        interfaze.addJavaDocLine("/**");
        String desc = " * <p>对应表: " +
                introspectedTable.getFullyQualifiedTable() +
                "</p>";
        interfaze.addJavaDocLine(desc);
        String entity = " * <p>对应实体: " +
                introspectedTable.getBaseRecordType() +
                "</p>";
        interfaze.addJavaDocLine(entity);
        interfaze.addJavaDocLine(" *");
        interfaze.addJavaDocLine(" * @author " + author);
        interfaze.addJavaDocLine(" * @since " + DateUtils.getDateStr());
        interfaze.addJavaDocLine(" */");
        return true;
    }
}
